package com.zxy.ziems.server.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxy.ziems.server.constant.AuthType;
import com.zxy.ziems.server.constant.ResourceType;
import com.zxy.ziems.server.domain.Resource;
import com.zxy.ziems.server.domain.RoleAuthRel;
import com.zxy.ziems.server.domain.RoleResourceRel;
import com.zxy.ziems.server.dto.RoleAuthRelDto;
import com.zxy.ziems.server.mapper.RoleAuthRelMapper;
import com.zxy.ziems.server.service.ResourceService;
import com.zxy.ziems.server.service.RoleAuthRelService;
import com.zxy.ziems.server.service.RoleResourceRelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName RoleAuthRelServiceImpl
 * @Description
 * @Author 邓伟
 * @Date 2023/11/22 16:53
 * @Version 1.0
 */
@Service
public class RoleAuthRelServiceImpl extends ServiceImpl<RoleAuthRelMapper, RoleAuthRel> implements RoleAuthRelService {

    @Autowired
    private RoleAuthRelMapper roleAuthRelMapper;
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private RoleResourceRelService roleResourceRelService;

    @Override
    public void bindDataAuth(RoleAuthRelDto roleAuthRelDto) {
        //删除旧数据
        QueryWrapper<RoleAuthRel> query = new QueryWrapper<>();
        query.lambda().eq(RoleAuthRel::getRoleId,roleAuthRelDto.getRoleId());
        roleAuthRelMapper.delete(query);
        List<RoleAuthRel> roleAuthRelList = roleAuthRelDto.getRoleAuthRelList();
        for (RoleAuthRel roleAuthRel : roleAuthRelList) {
            roleAuthRel.setRoleId(roleAuthRelDto.getRoleId());
        }
        saveBatch(roleAuthRelList);
    }

    @Override
    public List<RoleAuthRel> getRoleAuth(String roleId) {
        List<RoleAuthRel> returnDataList = new ArrayList<>();
        //查询角色关联的菜单id
        QueryWrapper<RoleResourceRel> query = new QueryWrapper<>();
        query.lambda().eq(RoleResourceRel::getRoleId,roleId);
        List<RoleResourceRel> list = roleResourceRelService.list(query);
        if (CollectionUtil.isEmpty(list)) {
            return returnDataList;
        }
        List<String> resourceIdList = list.stream().map(RoleResourceRel::getResourceId).toList();
        //查询所有的菜单信息
        QueryWrapper<Resource> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Resource::getResourceType, ResourceType.MENU_TYPE)
                .in(Resource::getResourceId,resourceIdList);
        List<Resource> resourceList = resourceService.list(queryWrapper);
        //查询已经配置了数据权限的菜单信息
        QueryWrapper<RoleAuthRel> queryAuth = new QueryWrapper<>();
        queryAuth.lambda().eq(RoleAuthRel::getRoleId,roleId);
        List<RoleAuthRel> authRelList = roleAuthRelMapper.selectList(queryAuth);
        if (CollectionUtil.isEmpty(authRelList)) {
            for (Resource resource : resourceList) {
                RoleAuthRel roleAuthRel = new RoleAuthRel();
                roleAuthRel.setResourceId(resource.getResourceId());
                roleAuthRel.setResourceName(resource.getResourceName());
                roleAuthRel.setAuthType(AuthType.LOCATION_TYPE);
                returnDataList.add(roleAuthRel);

            }
            return returnDataList;
        }
        Map<String, RoleAuthRel> relMap = authRelList.stream().collect(Collectors.toMap(RoleAuthRel::getResourceId, v1 -> v1));
        //合并数据
        for (Resource resource : resourceList) {
            RoleAuthRel roleAuthRel = relMap.get(resource.getResourceId());
            if (ObjectUtil.isEmpty(roleAuthRel)) {
                RoleAuthRel data = new RoleAuthRel();
                data.setResourceId(resource.getResourceId());
                data.setResourceName(resource.getResourceName());
                data.setAuthType(AuthType.LOCATION_TYPE);
                returnDataList.add(roleAuthRel);
            }else {
                roleAuthRel.setResourceName(resource.getResourceName());
                returnDataList.add(roleAuthRel);
            }
        }
        return returnDataList;
    }
}
